<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use manniu\model\Base;

class AdminRole extends Base
{
    // 数据完成
    protected $insert = ['parent_id' => 0,'rules'=>'1,2,3,4'];
    // 系统权限 - 获取器
    public function getSystemAttr($value){
    	return mn_string2array($value);
    }
    // 系统权限 - 修改器
    public function setSystemAttr($value){
    	return mn_array2string($value);
    }
    // 模块权限 - 获取器
    public function getModuleAttr($value){
    	return mn_string2array($value);
    }
    // 模块权限 - 修改器
    public function setModuleAttr($value){
    	return mn_array2string($value);
    }

    /**
     * 查询单个管理角色
     * @param $id
     */
    public static function getRole($id){
        $role = self::get($id);
        return $role;
    }

    /**
     * 查询所有管理角色
     * @return array
     */
    public static function getRoleAll(){
        $roles = [];
        $result = self::all();
        if($result){
            foreach ($result as $key => $role){
                $roles[$role->getData('id')] = $role;
            }
        }
        return $roles;
    }

	/**
     * 更新缓存
     */
	public function cache(){
        Cache::set('admin_role',NULL);
		$data = [];
		foreach ($this->order('id', 'asc')->select() as $key => $role) {
			$data[$role->id] = $role->data;
		}
		if(!empty($data)){
            Cache::set('admin_role',$data);
        }
	}
}